package com.etc.controller;

import com.etc.entity.Admins;
import com.etc.service.AdminServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
@SessionAttributes(value = "admin")
public class AdminController {
    @Autowired
    AdminServiceImpl adminServiceImpl;

    /**
     * 注册
     *
     * @param admins admin信息
     * @return modelandview
     */
    @RequestMapping("/register")
    public ModelAndView register(Admins admins) {
        ModelAndView modelAndView = new ModelAndView();
        boolean b = adminServiceImpl.register(admins);
        if (b) {
            Admins login = adminServiceImpl.login(admins.getAdmin_acount(), admins.getAdmin_password());
            modelAndView.addObject("admin", login);
            modelAndView.setViewName("back/usersList");
        } else {
            modelAndView.setViewName("back/adminRegister");
        }
        return modelAndView;
    }

    /**
     * 跳转到注册
     *
     * @return String
     */
    @RequestMapping("/toRegister")
    public String toRegister() {
        return "back/adminRegister";
    }

    /**
     * 登录
     *
     * @param admin_acount   账号
     * @param admin_password 密码
     * @param session        session
     * @return String
     */
    @RequestMapping("/login")
    public String login(String admin_acount, String admin_password, HttpSession session) {
        Admins login = adminServiceImpl.login(admin_acount, admin_password);
        if (login != null) {
            session.setAttribute("admin", login);
            return "back/usersList";
        } else {
            return "forward:/adminindex.jsp";
        }
    }

    /**
     * 更新
     *
     * @param admins  admin信息
     * @param session session
     * @return boolean
     */
    @RequestMapping("/updateByAdmin")
    @ResponseBody
    public boolean updateByAdmin(Admins admins, HttpSession session) {
        boolean b = adminServiceImpl.updateByAdmin(admins);
        if (b) {
            Admins login = adminServiceImpl.login(admins.getAdmin_acount(), admins.getAdmin_password());
            session.setAttribute("admin", login);
        } else {

        }
        return b;
    }

    /**
     * 删除
     *
     * @param admin_no 编号
     * @return boolean
     */
    @RequestMapping("/deleteByAdmin")
    @ResponseBody
    public boolean deleteByAdmin(Integer admin_no) {
        boolean b = adminServiceImpl.deleteByAdmin(admin_no);
        return b;
    }

    /**
     * 查询所有
     *
     * @param pageNum  页数
     * @param pageSize 数量
     * @return pageinfo
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public PageInfo<Admins> queryAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<Admins> pageInfo = adminServiceImpl.queryAll(pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 跳转到管理列表
     *
     * @return String
     */
    @RequestMapping("/toAdminList")
    public String toAdminList() {
        return "back/adminList";
    }

    /**
     * 跳转到图表
     *
     * @return String
     */
    @RequestMapping("/toCharts")
    public String toCharts() {
        return "back/charts";
    }

    /**
     * 跳转到电影列表
     *
     * @return String
     */
    @RequestMapping("/toMoviesDetail")
    public String toMoviesDetail() {
        return "back/moviesDetail";
    }

    /**
     * 跳转到增加电影
     *
     * @return String
     */
    @RequestMapping("/toAddMovies")
    public String toAddMovies() {
        return "back/addMovies";
    }

    /**
     * 跳转到用户列表
     *
     * @return String
     */
    @RequestMapping("/toUsersList")
    public String toUsersList() {
        return "back/usersList";
    }

    /**
     * 跳转到添加场次
     *
     * @return String
     */
    @RequestMapping("/toAddShowing")
    public String toAddShowing() {
        return "back/addShowing";
    }

    /**
     * 跳转到场次列表
     *
     * @return String
     */
    @RequestMapping("/toShowingList")
    public String toShowingList() {
        return "back/showingList";
    }

    /**
     * 跳转到添加展厅
     *
     * @return String
     */
    @RequestMapping("/toAddCinemas")
    public String toAddCinemas() {
        return "back/addCinemas";
    }

    /**
     * 跳转到展厅列表
     *
     * @return String
     */
    @RequestMapping("/toCinemasList")
    public String toCinemasList() {
        return "back/cinemasList";
    }

    /**
     * 跳转到添加类型
     *
     * @return String
     */
    @RequestMapping("/toAddType")
    public String toAddType() {
        return "back/addType";
    }

    /**
     * 跳转到添加电影类型
     *
     * @return String
     */
    @RequestMapping("/toAddCate")
    public String toAddCate() {
        return "back/addCate";
    }
}
